﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.IO;
using NormReference.DAL;
using NormReference.Services;

namespace NormReference.Models
{
    public class ComparisonGroup
    {
        public int ID { get; set; }
        public string name { get; set; }
        public DateTime dateCreated { get; set; }
        //public int normReferenced {get; set;}

        public virtual ICollection<DepartmentFilter> departmentFilters { get; set; }
        public virtual ICollection<DivisionFilter> divisionFilters { get; set; }
        public virtual ICollection<FacultyReport> facultyReports { get; set; }

        public List<Int32[]> responseCounts = new List<Int32[]>();
        
        public int calculateRowSum(int index)
        {
            Int32[] row = responseCounts.ElementAt(index);
            int sum = 0;

            for(int i = 0; i<= row.Length; i++)
            {
                sum += row[i];
            }

            return sum;
        }

        public string toString()
        {
            return name;
        }     
 
        public string generateSQL()
        {
            string output = "";
            int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28};
            
            foreach(int i in array)
            {
                Int32[] intArray = new int[5];

                for(int j = 1; j<= 5; j++)
                {
                    string result = " select count(core" + i + ") from courseevaluation"; 
                    if(departmentFilters.Count  == 0 && divisionFilters.Count == 0)
                    {

                    }
                    else if(departmentFilters.Count == 0 && divisionFilters.Count > 0)
                    {
                        result += " where division in " + printDivisionFiltersSQL();
                    }
                    else if (departmentFilters.Count > 0 && divisionFilters.Count == 0)
                    {
                        result += " where department in " + printDepartmentFiltersSQL();
                    }
                    else 
                    {
                        result += " where department in " + printDepartmentFiltersSQL() +
                            " and division in " + printDivisionFiltersSQL();
                    }

                    result += " and core" + i + " = '" + j + "' ";

                    Logger.getInstance().printToLogFile("SQL Query Attempt = " + result);
                    NormReferenceContext db = new NormReferenceContext();
                    var sqlResult = db.Database.SqlQuery<int>(result).ToList();
                    Logger.getInstance().printToLogFile("SQL Result = " + sqlResult[0].ToString());
                    intArray[j - 1] = sqlResult[0];
                }

                responseCounts.Add(intArray);    
              }

            output += "SUCCESS!!!";
            return output;
        }

        public string printDepartmentFiltersSQL()
        {
            string output = "(";

            foreach(DepartmentFilter dept in departmentFilters)
            {   
                if (dept == departmentFilters.Last())
                {
                    output  +=  "\'" + dept.department.code + "\'";
                }
                else 
                {
                    output += "\'" + dept.department.code + "\'" + ", ";
                }
            }

            output += ")";
            return output;
        }

        public string printDivisionFiltersSQL()
        {
            string output = "(";

            foreach (DivisionFilter div in divisionFilters)
            {
                if (div == divisionFilters.Last())
                {
                    output += "\'" + div.division.code + "\'";
                }
                else
                {
                    output += "\'" + div.division.code + "\'" + ", ";
                }
            }

            output += ")";
            return output;
        }

        public String printResponseCounts()
        {
            Logger.getInstance().printToLogFile("PRINTING RESPONSE COUNT!");
            string output = "";

            if (responseCounts.Count == 0)
            {
                return "Response Counts Array is empty. ";
            }

            foreach (int[] row in responseCounts)
            {
                output += "(";
                for (int i = 0; i < 5; i++)
                {
                    if (i == 4)
                    {
                        output += "" + row[i];
                    }
                    else
                    {
                        output += "" + row[i] + ", ";
                    }
                }
                output += ") \n";
            }

            return output;
        }

        public String printProbabilities()
        {
            Logger.getInstance().printToLogFile("PRINTING RESPONSE COUNT!");
            string output = "";
            
            if (responseCounts.Count == 0)
            {
                return "Response Counts Array is empty. ";
            }

            foreach (int[] row in responseCounts)
            {
                decimal sum = 0.0m;
                for (int i = 0; i < 5; i++ )
                {
                    decimal value = Convert.ToDecimal(row[i]);
                    sum += value;
                    Logger.getInstance().printToLogFile("Sum Value: " + sum.ToString());
                }
                
                for (int i = 0; i < 5; i++)
                {
                    decimal result = 0.0m;
                    decimal probSum = 0.0m;

                    if (i == 4)
                    {
                        decimal value = Convert.ToDecimal(row[i]);
                        result = value/ sum;
                        output += "" + result;
                        probSum += value;
                        Logger.getInstance().printToLogFile("Value: " + value.ToString());
                        Logger.getInstance().printToLogFile("Result: " + result.ToString());
                        Logger.getInstance().printToLogFile("Current ProbSum: " + probSum.ToString());
                    }
                    else
                    {
                        decimal value = Convert.ToDecimal(row[i]);
                        result = value / sum;
                        output += "" + result+ ", ";
                        probSum += value;
                        Logger.getInstance().printToLogFile("Value: " + value.ToString());
                        Logger.getInstance().printToLogFile("Result: " + result.ToString());
                        Logger.getInstance().printToLogFile("Current ProbSum: " + probSum.ToString());
                    }
                }
                output += "\n";
            }

            return output;
        }
     }
}